package com.example.databindingdemo.adapter;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.example.databindingdemo.R;
import com.example.databindingdemo.bean.Book;
import com.example.databindingdemo.databinding.LayoutItemBinding;

import java.util.List;

/**
 * @author: njb
 * @date: 2022/9/18 17:16
 * @desc:
 */
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
    private List<Book> bookList;

    public RecyclerViewAdapter(List<Book> books){
        this.bookList = books;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutItemBinding layoutItemBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),R.layout.layout_item,parent,false);

        return new MyViewHolder(layoutItemBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
       Book book = bookList.get(position);
       holder.layoutItemBinding.setBook(book);
    }

    @Override
    public int getItemCount() {
        return bookList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        LayoutItemBinding layoutItemBinding;

        public MyViewHolder(LayoutItemBinding itemView) {
            super(itemView.getRoot());
            layoutItemBinding = itemView;
        }
    }
}
